package com.luzhikun.jackson.dao;

import com.luzhikun.jackson.entity.Privince;

import java.sql.*;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class ProvinceDao {

    // 根据id获取一个完整的Province对象
    public Privince queryProvinceNameById(Integer id){
        Connection connection=null;
        PreparedStatement pst=null;
        ResultSet rs=null;
        String sql="";

        String url="jdbc:mysql://localhost:3306/springdb";
        String username="root";
        String password="140489lu";
        String name="";
        Privince privince=null;
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(url,username,password);
            sql="select id,name,jiancheng,shenghui from province where id=?";
            pst=connection.prepareStatement(sql);
            pst.setInt(1,id);
            rs= pst.executeQuery();
            if (rs.next()){
                privince=new Privince();
                privince.setId(rs.getInt("id"));
                privince.setName(rs.getString("name"));
                privince.setJiancheng(rs.getString("jiancheng"));
                privince.setShenghui(rs.getString("shenghui"));
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }if (pst!=null){
                try {
                    pst.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

        return privince;
    }


}
